;var module = angular.module('CtrlsContent');
module.controller('BLSCtrl',['$scope','$ionicPopup','api_other','$state','$timeout','$filter',function($scope,$ionicPopup,api_other,$state,$timeout,$filter){
	$scope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
		if(!$scope.event.loginPop()){
			event.preventDefault();
		}

    })
	$scope.model = {
		banner:'images/img/bg_index_banner.jpg',
		link:'',
		testTime:'',
		showTime:0,
		title:localStorage.bls_catname
	};
	$scope.event = {
		weixinLogin:function(){
			location.href=api_other.userLogin();
		},
		isFreeTiku:function(){
			var obj = {
				catId:localStorage.catId,
				userId:localStorage.bls_userId
			};
			var promise = api_other.treeNum(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					if(data.returnMsg == "success"){
						$scope.eleStatus.isTestSelectShow = true;$scope.eleStatus.mask = true;
					}
					if(data.hasOwnProperty('returnData') && data.returnData.hasOwnProperty('num') ){
						if(parseInt(data.returnData.num) >=parseInt(data.returnData.total)){
							$scope.eleStatus.isTestSelectShow = false;$scope.eleStatus.mask = false;
							//  confirm 对话框
				             var confirmPopup = $ionicPopup.confirm({
				               title: '提示',
				               template: '您可以免费题数已经做完，是否购买?',
				                buttons: [
					               { text: '否' },
					               {
					                 text: '<b>是</b>',
					                 type: 'button-positive',
					                 onTap: function(e) {
					                   $state.go('tikubuy');
					                 }
					               }
	             				]
				             });
						}
					}
				}
			})
		},
		isgoumaiTiku:function(){
			var obj = {
				catId:localStorage.catId,
				userId:localStorage.bls_userId
			};
			var promise = api_other.treeNum(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					if(data.returnMsg == "success" && !data.hasOwnProperty('returnData')){
						$scope.eleStatus.isgoumaiTiku = false;
					}else if(data.returnMsg == "success" && data.hasOwnProperty('returnData') && data.returnData.hasOwnProperty('num')){
						$scope.eleStatus.freeRela = (parseInt(data.returnData.total) - parseInt(data.returnData.num))/parseInt(data.returnData.total);
					}
				}
			})
		},
		tikugoumai:function(){
			var promise = api_other.getBanner(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					$scope.model.banner = "http://"+data.returnData.cover;
					$scope.model.link = "http://"+data.returnData.link;
				}
			})
		},
		loginPop:function(){
			if(location.search.substring(8)){
				localStorage.bls_userId = location.search.substring(8)
				return true;
			}else{
				$scope.eleStatus.isLoginShow = true;
				$scope.eleStatus.mask = true;
				return false;
			}
			
		},

		getBanner:function(){
			var obj = {
				cat_id:localStorage.catId,
				type:1
			};
			var promise = api_other.getBanner(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					$scope.model.banner = "http://"+data.returnData.cover;
					$scope.model.link = "http://"+data.returnData.link;
				}
			})
		},
		closePop:function(){
			angular.forEach($scope.eleStatus, function(data,index,array){
				array[index] = false;
			});
		},
		setTime:function(){
			var self = this;
			var setTime = (new Date($scope.model.testTime)).getTime();
			var currTiem = new Date().getTime();
			if(setTime<currTiem){
				$scope.model.testTime = '';
				return ;
			}
			var obj = {
				userId:localStorage.bls_userId,
				time:$filter('date')($scope.model.testTime,'yyyy-MM-dd')
			}
			var promise = api_other.setTime(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					self.getTime();
				}else{
					alert('设置时间失败')
				}
			})
		},
		getTime:function(){
			var obj = {
				userId:localStorage.bls_userId
			};
			var promise = api_other.getTime(obj);
			promise.success(function(data){
				if(data.returnCode == 0){
					$scope.model.showTime = data.returnData.days;
				}else{
					alert('获取时间失败');
				}
			})
		}
		
	};
	$scope.eleStatus = {
		mask:false,
		isTestSelectShow:false,
		isLoginShow:false,
		isgoumaiTiku:true,
		freeRela:0,
	};
	$scope.blsPop = {
		login:''
	};
	(function(){
		$scope.event.getBanner();
		$scope.event.getTime();
		$scope.event.isgoumaiTiku();
		if(!localStorage.catId){
			$state.go('testList');
		}
	})();
}]);